package TOP101;

/**
 * @Question BM7
 * @Date 2022/11/2 16:33
 * @Solution
 */
public class BM7 {
    public ListNode EntryNodeOfLoop(ListNode pHead) {
        if (pHead == null) {
            return null;
        }
        ListNode fast = pHead;
        ListNode slow = pHead;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                fast=pHead;
                while (fast != null && fast.next != null) {
                    if(fast==slow) {
                        return fast;
                    }
                    fast = fast.next;
                    slow = slow.next;
                }
            }
        }
        return null;
    }
}
